package com.zx.cloud.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.zx.cloud.domain.Resource;

/** 
 * 树形结构构建
 */  
public class TreeNode implements Serializable {  
      
    private static final long serialVersionUID = 1L;  
    /** 
     * 节点ID 
     */  
    public String id;  
    /** 
     * 节点内容 
     */  
    public String text;  
    /** 
     * 父节点编号 
     */  
    public String parentId;  
    
    public String state="closed";
    /** 
     * 孩子节点列表 
     */  
    public List<TreeNode> children = new ArrayList<TreeNode>();  
      
    /** 
     * 添加子节点 
     *  
     * @param node 子节点 
     */  
    public void addChild(TreeNode node) {  
        children.add(node);  
    } 
    
    public int childLen() {  
        return children.size();
    } 
      
    /** 
     * 说明方法描述：将list转为树tree结构 
     */  
    public static List<TreeNode> tree(List<TreeNode> allRrecords) {  
        List<TreeNode> listParentTreeNode = new ArrayList<TreeNode>();  
        List<TreeNode> listNotParentTreeNode = new ArrayList<TreeNode>();  
        // 第一步：遍历allRrecords保存所有数据的uuid用于判断是不是根节点  
        Map<String, String> mapAllUuid = new HashMap<String, String>();  
        Map<String, TreeNode> allTreeNodeMap = new HashMap<String, TreeNode>();  
        for (TreeNode record : allRrecords) {  
            mapAllUuid.put(record.id, record.id);  
            allTreeNodeMap.put(record.id, record);  
        }  
        // 第二步：遍历allRrecords找出所有的根节点和非根节点  
        if (allRrecords != null && allRrecords.size() > 0) {  
            for (TreeNode record : allRrecords) {  
                if (StringUtils.isBlank(record.parentId)  
                    || !mapAllUuid.containsKey(record.parentId)) {  
                    listParentTreeNode.add(record);  
                } else {  
                    listNotParentTreeNode.add(record);  
                }  
            }  
        }  
        // 第三步： 递归获取所有子节点  
        if (listParentTreeNode.size() > 0) {  
            for (TreeNode record : listParentTreeNode) {  
                // 添加所有子级  
                record.children.addAll(getTreeChildTreeNode(listNotParentTreeNode, record.id));  
            }  
        }  
        return listParentTreeNode;  
    }  

    /** 
     * 说明方法描述：递归查询子节点 
     *  
     * @param childList 子节点 
     * @param parentUuid 父节点id 
     * @return 
     * @time 2016年5月10日 下午3:29:35 
     * @author yangdong 
     */  
    private static List<TreeNode> getTreeChildTreeNode(List<TreeNode> childList, String parentUuid) {  
        List<TreeNode> listParentTreeNode = new ArrayList<TreeNode>();  
        List<TreeNode> listNotParentTreeNode = new ArrayList<TreeNode>();  
        // 遍历tmpList，找出所有的根节点和非根节点  
        if (childList != null && childList.size() > 0) {  
            for (TreeNode record : childList) {  
                // 对比找出父节点  
                if (StringUtils.equals(record.parentId, parentUuid)) {  
                    listParentTreeNode.add(record);  
                } else {  
                    listNotParentTreeNode.add(record);  
                }  
            }  
        }  
        // 查询子节点  
        if (listParentTreeNode.size() > 0) {  
            for (TreeNode record : listParentTreeNode) {  
                // 递归查询子节点  
                record.children.addAll(getTreeChildTreeNode(listNotParentTreeNode, record.id));  
            }  
        }  
        return listParentTreeNode;  
    }  
    
    /** 
     * 说明方法描述：将list转为树tree结构 
     */  
    public static List<Resource> treeResource(List<Resource> allRrecords) {  
        List<Resource> listParentTreeNode = new ArrayList<Resource>();  
        List<Resource> listNotParentTreeNode = new ArrayList<Resource>();  
        // 第一步：遍历allRrecords保存所有数据的uuid用于判断是不是根节点  
        Map<Long, Long> mapAllUuid = new HashMap<Long, Long>();  
        Map<Long, Resource> allTreeNodeMap = new HashMap<Long, Resource>();  
        for (Resource record : allRrecords) {  
            mapAllUuid.put(record.getId(), record.getId());  
            allTreeNodeMap.put(record.getId(), record);  
        }  
        // 第二步：遍历allRrecords找出所有的根节点和非根节点  
        if (allRrecords != null && allRrecords.size() > 0) {  
            for (Resource record : allRrecords) {  
                if (record.getParentId()!=null
                    || !mapAllUuid.containsKey(record.getParentId())) {  
                    listParentTreeNode.add(record);  
                } else {  
                    listNotParentTreeNode.add(record);  
                }  
            }  
        }  
        // 第三步： 递归获取所有子节点  
        if (listParentTreeNode.size() > 0) {  
            for (Resource record : listParentTreeNode) {  
            	if(record.getResources()!=null){
            		record.getResources().addAll(getTreeResourceChildTreeNode(listNotParentTreeNode, record.getId()));  
				}else{
					List<Resource> resources=new ArrayList<Resource>();
					resources.add(record);
					record.setResources(resources);
				}
            }  
        }  
        return listParentTreeNode;  
    }  

    /** 
     * 说明方法描述：递归查询子节点 
     *  
     * @param childList 子节点 
     * @param parentUuid 父节点id 
     * @return 
     * @time 2016年5月10日 下午3:29:35 
     * @author yangdong 
     */  
    private static List<Resource> getTreeResourceChildTreeNode(List<Resource> childList, Long parentUuid) {  
        List<Resource> listParentTreeNode = new ArrayList<Resource>();  
        List<Resource> listNotParentTreeNode = new ArrayList<Resource>();  
        // 遍历tmpList，找出所有的根节点和非根节点  
        if (childList != null && childList.size() > 0) {  
            for (Resource record : childList) {  
                // 对比找出父节点  
                if (record.getParentId().longValue()== parentUuid.longValue()) {  
                    listParentTreeNode.add(record);  
                } else {  
                    listNotParentTreeNode.add(record);  
                }  
            }  
        }  
        // 查询子节点  
        if (listParentTreeNode.size() > 0) {  
            for (Resource record : listParentTreeNode) {  
                // 递归查询子节点  
            	if(record.getResources()!=null){
            		record.getResources().addAll(getTreeResourceChildTreeNode(listNotParentTreeNode, record.getId()));  
				}else{
					List<Resource> resources=new ArrayList<Resource>();
					resources.add(record);
					record.setResources(resources);
				}
                
            }  
        }  
        return listParentTreeNode;  
    }  
  
  
}  